package java.util.stream.volatiled;

/**
 * @author fangjiaxiaobai@gmail.com
 * @date 2020-05-06
 * @since 1.0.0
 */
public class Demo {


    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();

        Thread thread = new Thread(() -> {
            for (int i = 0; i < 100000; i++) {
                counter.increment();
            }
        });
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 100000; i++) {
                counter.increment();
            }
        });

        thread.start();
        thread1.start();

        thread.join();
        thread1.join();
        System.out.println(counter.getCount());

    }


}

class Counter {

    private volatile int count = 0;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void increment(){
        this.count++;
    }
}